package net.fltx.spai.controller;

import javafx.concurrent.Worker;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Text;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;

import java.net.URL;
import java.util.ResourceBundle;

public class Window implements Initializable {

    @FXML
    private WebView webView;
    private WebEngine webEngine;
    @FXML
    private TextField _href;
    @FXML
    private Button _back,  _forward, _reload, _load, _option;
    @FXML
    private Label _label;
    @FXML
    private ProgressBar _pBar;
    public void initialize(URL location, ResourceBundle resources) {
        webEngine=webView.getEngine();
        webEngine.load(_href.getText());
        Worker<Void> loadWorker = webEngine.getLoadWorker();
        loadWorker.stateProperty().addListener((observable, oldValue, newValue) -> {
            if(Worker.State.SUCCEEDED==newValue){
                _href.setText(webEngine.getLocation());
            }
        });

        System.out.println(location);
        System.out.println(resources);

        webEngine.setOnStatusChanged(event -> {
            _label.setText(event.getData());
        });



    }
    public WebEngine getWebEngine() {
        return webEngine;
    }
    /**
     * 后退
     */
    @FXML
    public void back(ActionEvent event){
        webEngine.getHistory().go(-1);
    }

    public void forward(ActionEvent event){
        webEngine.getHistory().go(1);
    }
    public void reload(ActionEvent event){
        webView.getEngine().reload();
    }
    public void load(ActionEvent event){
        webEngine.load(_href.getText());
    }
    public void option(ActionEvent event){

    }


}
